﻿using DbUtility;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.DAL
{
    public class ProductStoreDAL:BaseDAL<ProductStoreInfo>
    {

        /// <summary>
        /// 添加产品入库(仓库里并无该商品在该仓库分区的存储记录)
        /// </summary>
        /// <param name="psInfo"></param>
        /// <returns></returns>
          public bool AddProductStore(ProductStoreInfo psInfo)
        {
            string cols = CreateSql.GetColsString<ProductStoreInfo>("IsDeleted");
            string reCols = CreateSql.GetColsString<ProductInStoreRecordInfo>("IsDeleted,InStoreTime");
            //添加入库
            List<CommandInfo> comList = new List<CommandInfo>();
            SqlModel addStore = CreateSql.GetInsertSqlAndParas(psInfo, cols, 0);
            comList.Add(new CommandInfo()
            {
                CommandText = addStore.Sql,
                IsProc = false,
                Paras = addStore.SqlParaArray
            });
            //入库记录信息
            ProductInStoreRecordInfo record = new ProductInStoreRecordInfo()
            {
                ProductId = psInfo.ProductId,
                SRegionId = psInfo.SRegionId,
                ProductCount = psInfo.ProductCount
            };
            //添加本次入库记录
            SqlModel addRecord = CreateSql.GetInsertSqlAndParas(record, reCols,0);
            comList.Add(new CommandInfo()
            {
                CommandText = addRecord.Sql,
                IsProc = false,
                Paras = addRecord.SqlParaArray
            });
            return SqlHelper.ExecuteTrans(comList);
        }

        /// <summary>
        /// 更新商品入库（入库的商品在仓库中已有存储记录）
        /// </summary>
        /// <param name="psInfo"></param>
        /// <returns></returns>
        public bool UpdateProductStore(ProductStoreInfo psInfo)
        {
            //修改入库（在原来的基础上累加）
            List<CommandInfo> comList = new List<CommandInfo>();
            string upSql=$"update ProductStoreInfos set ProductCount=ProductCount+{psInfo.ProductCount} where SRegionId={psInfo.SRegionId} and ProductId={psInfo.ProductId}";
            
            comList.Add(new CommandInfo()
            {
                CommandText = upSql,
                IsProc = false
            });
            //入库记录信息
            ProductInStoreRecordInfo record = new ProductInStoreRecordInfo()
            {
                ProductId = psInfo.ProductId,
                SRegionId = psInfo.SRegionId,
                ProductCount = psInfo.ProductCount
            };
            //添加本次入库记录
            string reCols = CreateSql.GetColsString<ProductInStoreRecordInfo>("IsDeleted,InStoreTime");
            SqlModel addRecord = CreateSql.GetInsertSqlAndParas(record, reCols, 0);
            comList.Add(new CommandInfo()
            {
                CommandText = addRecord.Sql,
                IsProc = false,
                Paras = addRecord.SqlParaArray
            });
            return SqlHelper.ExecuteTrans(comList);
        }

        /// <summary>
        /// 判断该商品在该分区中是否存在存储记录
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public bool ExitsStoreRecord(int productId,int regionId)
        {
            string strWhere = $"ProductId={productId} and SRegionId={regionId}";
            return Exists(strWhere);
        }

        /// <summary>
        /// 获取商品库存列表
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="regionId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public List<ProductStoreInfo> GetProductStoreList(int storeId,int regionId,int productId)
        {
            string strWhere = "IsDeleted=0";
            if (storeId > 0)
                strWhere += $" and StoreId={storeId}";
            if (regionId > 0)
                strWhere += $" and SRegionId={regionId}";
            if (productId > 0)
                strWhere += $" and ProductId={productId}";
            string cols = CreateSql.GetColsString<ProductStoreInfo>("IsDeleted");
            return GetModelList(strWhere, cols);
        }

    }
}
